package com.pineapple.module.web.login;

import java.sql.Connection;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.pineapple.module.core.login.domain.LoginModel;
import com.pineapple.module.core.login.domain.User;
import com.pineapple.module.core.login.service.LoginManager;
import com.pineapple.module.util.connection.DBCPConnection;

public class LoginAction extends ActionSupport implements ModelDriven<LoginModel> {

	private static final long serialVersionUID = -3661000161146714152L;
	LoginModel model = new LoginModel();



	public String login(){
		LoginManager manager = new LoginManager();

		String returnPage = null;
		try(Connection conn= new DBCPConnection().getConnection()){
			User user = manager.login(model.getCriteria(), conn);

			if(user.getUserId() == null){
				ServletActionContext.getRequest().setAttribute("message", getText("0001"));
				returnPage = "init";
			}else{
				HttpSession session = ServletActionContext.getRequest().getSession();
				session.setAttribute("user", user);
				returnPage = "success";
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return returnPage;
	}

    public String logout() {
    	HttpSession session = ServletActionContext.getRequest().getSession();
    	session.removeAttribute("user");
    	session.removeAttribute("area");
		return "init";

	}

	public LoginModel getModel() {
		// TODO Auto-generated method stub
		return model;
	}

}
